package JAVA300.第九章容器;

import java.util.HashMap;

/**
 * 手动实现HashSet
 * 深层理解hashset底层原理
 *
 */
public class TestMyHashSet {

    HashMap map;
    private static final Object PRESENT = new Object();

    public TestMyHashSet() {

        map = new HashMap();
    }

    public void add(Object obj){
        map.put(obj,PRESENT);
    }

    public int size(){
        return map.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('{');

        for (Object key:map.keySet()){
            sb.append(key+",");
        }
        sb.setCharAt(sb.length()-1,'}');
        return sb.toString();
    }

    public static void main(String[] args) {
        TestMyHashSet hashSet = new TestMyHashSet();
        hashSet.add("aa");
        hashSet.add("bb");

        hashSet.add("cc");

        System.out.println(hashSet);

    }
}
